package com.zh.springcloud.controller;

import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description: 资源
 * @ClassName UserController
 * @date: 2021.10.26 20:27
 * @Author: zhanghang
 */
@RestController
@RequestMapping("/user")
public class UserController {

//	@GetMapping("/getLoginUser")
//	public Object getLoginUser(Authentication authentication){
//		return authentication.getPrincipal();
//	}

	@GetMapping("/getLoginUser")
	public Object getLoginUser(){
		return "hello oauth2";
	}

	@GetMapping("/getLoginUser1")
	public Object getLoginUser1(Authentication authentication, HttpServletRequest request){
		System.out.println(authentication.getName());
		System.out.println(authentication.getDetails());
		String authorization = request.getHeader("Authorization");
		System.out.println(authorization.lastIndexOf("bearer"));
		String bearer = authorization.substring(8);
		System.out.println(bearer);
		// 解析jwt
		return Jwts.parser().setSigningKey("123456".getBytes(StandardCharsets.UTF_8)).parse(bearer).getBody();
	}

	public static void main(String[] args) {
		String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(1638104288000L));
		System.out.println(format);
	}
}
